﻿namespace OnlineMediaAnalyzer
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using OnlineMediaAnalyzer.Models;
    using System.Collections.Generic;
    using System.Data.Entity.Infrastructure;
    using System.Diagnostics;

    //Tutorial: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application

    public class DataMigration
    {
        Dictionary<string, object> models = new Dictionary<string, object>();

        private string[] parties = { "Eesti Keskerakond", "Sotsiaaldemokraatlik Erakond", "Eesti Reformierakond", "Erakond Isamaa ja Res Publica Liit", "Erakond Eestimaa Rohelised" };

        private List<List<string>> partyKeywords = new List<List<string>>(){
            new List<string>(){"keskerakon","keskikud" },
            new List<string>(){"sotsiaaldemokraa","sde","sotsid"},
            new List<string>(){"reformierakon","reformika"},
            new List<string>(){"isamaa ja res","irl"},
            new List<string>(){"rohelised","roheliste"}
        };

        private List<List<string>> partyMembers = new List<List<string>>(){
            new List<string>(){"Edgar Savisaar","Enn Eesmaa","Kadri Simson","Mailis Reps","Kalev Kallo","Arvo Sarapuu","Taavi Aas","Aadu Must","Siret Kotka","Jüri Ratas","Priit Toobal","Tõnis Mölder","Marika Tuus-Laul","Jaanus Karilaid","Yana Toom","Mihhail Korb"},
            new List<string>(){"Sven Mikser","Indrek Saar","Andres Anvelt","Jevgeni Ossinovski","Heljo Pikhof","Karel Rüütli","Vadim Belobrovtsev","Tõnu Ints","Kalvi Kõva","Helmen Kütt","Reet Laja","Kajar Lember","Lauri Läänemets","Marianne Mikko","Eiki Nestor","Ivari Padar","Urve Palo","Maris Sild","Stanislav Tšerepanov","Rannar Vassivljev"},
            new List<string>(){"Taavi Rõivas","Kaja Kallas","Jürgen Ligi","Keit Pentus-Rosimannus","Urmas Paet","Urmas Kruuse","Rein Lang","Kalev Lillo","Hanno Pevkur","Laine Randjärv","Valdo Randpere","Jaanus Tamkivi","Kairi Uustulnd"},
            new List<string>(){"Urmas Reinsalu","Ene Ergma","Jaak Aaviksoo","Juhan Parts","Aivar Kokk","Andrus Saare","Annely Akkermann","Erki Nool","Helir-Valdor Seeder","Kaia Iva","Ken-Marti Vaher","Liisa-Ly Pakosta","Madis Kübar","Margus Tsahkna","Marko Mihkelson","Marko Pomerants","Peeter Laurson","Priit Sibul","Siim Kiisler","Sirle Rosenfeldt","Sven Sester","Tarmo Kruusimäe","Tunne Kelam","Tõnis Palts"},
            new List<string>(){"Aleksander Laane","Küllike Reimaa","Olev-Andres Tinn","Marko Kaasil","Kalju Valdmann","Andrei Gudim","Eero Uustalu","Urmas Sepp"}
        };

        public void Seed(OnlineMediaAnalyzerContext context)
        {
            AddRSSMaps(context);
            AddChannels(context);
            AddFeeds(context);
            AddParties(context);
        }

        private void AddRSSMaps(OnlineMediaAnalyzerContext context)
        {
            models.Add("RSSMap1", new RSSMap
            {
                Name = "PostimeesFormat",
                ItemMapping = "item",
                TitleMapping = "title",
                GuidMapping = "guid",
                UrlMapping = "link",
                DescriptionMapping = "description",
                AuthorMapping = "creator",
                TimeMapping = "pubDate",
                CategoryMapping = "category"
            });

            models.Add("RSSMapFeedburner", new RSSMap
            {
                Name = "PealinnFormat",
                ItemMapping = "item",
                TitleMapping = "title",
                GuidMapping = "origLink",
                UrlMapping = "link",
                DescriptionMapping = "description",
                AuthorMapping = "creator",
                TimeMapping = "pubDate",
                CategoryMapping = "category"
            });
            context.RSSMaps.AddOrUpdate(p => p.Name, (RSSMap)models["RSSMap1"]);
            context.RSSMaps.AddOrUpdate(p => p.Name, (RSSMap)models["RSSMapFeedburner"]);

        }

        private void AddChannels(OnlineMediaAnalyzerContext context)
        {
            models.Add("Postimees", new Channel
            {
                Name = "Postimees",
                Url = "http://www.postimees.ee"
            });
            models.Add("Pealinn", new Channel
            {
                Name = "Pealinn",
                Url = "http://www.delfi.ee"
            });
            models.Add("Delfi", new Channel
            {
                Name = "Delfi",
                Url = "http://www.pealinn.ee"
            });
            models.Add("Äripäev", new Channel
            {
                Name = "Äripäev",
                Url = "http://www.aripaev.ee"
            });
            models.Add("Eesti Päevaleht", new Channel
            {
                Name = "Eesti Päevaleht",
                Url = "http://www.epl.ee"
            });
            models.Add("Õhtuleht", new Channel
            {
                Name = "Õhtuleht",
                Url = "http://www.ohtuleht.ee"
            });
            models.Add("ERR Uudised", new Channel
            {
                Name = "ERR Uudised",
                Url = "http://uudised.err.ee"
            });
            models.Add("Eesti Ekspress", new Channel
            {
                Name = "Eesti Ekspress",
                Url = "http://www.ekspress.ee"
            });

            context.Channels.AddOrUpdate(p => p.Name, (Channel)models["Postimees"]);
            context.Channels.AddOrUpdate(p => p.Name, (Channel)models["Pealinn"]);
            context.Channels.AddOrUpdate(p => p.Name, (Channel)models["Delfi"]);
            context.Channels.AddOrUpdate(p => p.Name, (Channel)models["Äripäev"]);
            context.Channels.AddOrUpdate(p => p.Name, (Channel)models["Eesti Päevaleht"]);
            context.Channels.AddOrUpdate(p => p.Name, (Channel)models["Õhtuleht"]);
            context.Channels.AddOrUpdate(p => p.Name, (Channel)models["ERR Uudised"]);
            context.Channels.AddOrUpdate(p => p.Name, (Channel)models["Eesti Ekspress"]);
        }

        private void AddFeeds(OnlineMediaAnalyzerContext context)
        {

            //ChannelID = context.Channels.Where(c => c.Name=="Postimees").Select(c => c.ID).FirstOrDefault(),
            //RSSMapsID = context.RSSMaps.Where(c => c.Name=="PostimeesFormat").Select(c => c.ID).FirstOrDefault()
            context.Feeds.AddOrUpdate(p => p.Name,
               new Feed
               {
                   Name = "Postimees Online",
                   Url = "http://www.postimees.ee/rss",
                   Channel = (Channel)models["Postimees"],
                   RSSMap = (RSSMap)models["RSSMap1"]
               });

            context.Feeds.AddOrUpdate(p => p.Name,
               new Feed
               {
                   Name = "Pealinn",
                   Url = "http://www.pealinn.ee/rss.php?type=news",
                   Channel = (Channel)models["Pealinn"],
                   RSSMap = (RSSMap)models["RSSMap1"]
               });

            context.Feeds.AddOrUpdate(p => p.Name,
               new Feed
               {
                   Name = "Delfi",
                   Url = "http://feeds.feedburner.com/delfiuudised?format=xml",
                   Channel = (Channel)models["Delfi"],
                   RSSMap = (RSSMap)models["RSSMapFeedburner"]
               });
            context.Feeds.AddOrUpdate(p => p.Name,
               new Feed
               {
                   Name = "Äripäev",
                   Url = "http://www.aripaev.ee/mod/rss.xml",
                   Channel = (Channel)models["Äripäev"],
                   RSSMap = (RSSMap)models["RSSMap1"]
               });
            context.Feeds.AddOrUpdate(p => p.Name,
               new Feed
               {
                   Name = "Eesti Päevaleht",
                   Url = "http://feeds.feedburner.com/eestipaevaleht?format=xml",
                   Channel = (Channel)models["Eesti Päevaleht"],
                   RSSMap = (RSSMap)models["RSSMapFeedburner"]
               });
            context.Feeds.AddOrUpdate(p => p.Name,
               new Feed
               {
                   Name = "Õhtuleht",
                   Url = "http://www.ohtuleht.ee/rss",
                   Channel = (Channel)models["Õhtuleht"],
                   RSSMap = (RSSMap)models["RSSMap1"]
               });
            context.Feeds.AddOrUpdate(p => p.Name,
               new Feed
               {
                   Name = "ERR Uudised",
                   Url = "http://uudised.err.ee/rss",
                   Channel = (Channel)models["ERR Uudised"],
                   RSSMap = (RSSMap)models["RSSMap1"]
               });
            context.Feeds.AddOrUpdate(p => p.Name,
               new Feed
               {
                   Name = "Eesti Ekspress",
                   Url = "http://feeds.feedburner.com/EestiEkspressFeed?format=xml",
                   Channel = (Channel)models["Eesti Ekspress"],
                   RSSMap = (RSSMap)models["RSSMapFeedburner"]
               });
        }

        private void AddParties(OnlineMediaAnalyzerContext context)
        {
            for (int i = 0; i < parties.Length; i++)
            {
                Party part = new Party { Name = parties[i] };
                models.Add(parties[i], part);
                context.Parties.AddOrUpdate(p => p.Name, part);
                List<string> keywords = partyKeywords[i];
                foreach (string word in keywords)
                {
                    PartyKeyword k = new PartyKeyword { Keyword = word, Party = part };
                    context.PartyKeywords.AddOrUpdate(p => p.Keyword, k);
                    models.Add(word, k);
                }

                foreach (string member in partyMembers[i])
                {
                    PartyMember k = new PartyMember { Name = member, Party = part };
                    context.PartyMembers.AddOrUpdate(p => p.Name, k);
                    models.Add(member, k);
                }
            }
        }

    }
}
